package com.transportation.app.activity

import android.annotation.SuppressLint
import android.content.ClipData
import android.content.ClipboardManager
import android.content.Context
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.Switch
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.blankj.utilcode.util.DeviceUtils
import com.lxj.xpopup.XPopup
import com.transportation.app.R
import com.transportation.app.utils.AppUtils
import com.transportation.app.utils.ToastUtils


class SettingActivity : AppCompatActivity() {

    @SuppressLint("UseSwitchCompatOrMaterialCode")
    private var switch: Switch? = null

    companion object {
        val list = ArrayList<SitesInfo>()
    }

    @SuppressLint("SetTextI18n", "MissingInflatedId")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_setting)
        switch = findViewById(R.id.switch_printer_type)
        findViewById<TextView>(R.id.tv_version).text = "版本号：${AppUtils.getAppVersion(this)}"
        findViewById<TextView>(R.id.tv_device_tag).text = "设备标识:${DeviceUtils.getUniqueDeviceId()}（长按复制）"
        findViewById<ImageView>(R.id.iv_back_view).setOnClickListener {
            finish()
        }
        findViewById<TextView>(R.id.tv_location).setOnClickListener {
            showDialog()
        }

        findViewById<TextView>(R.id.tv_device_tag).setOnLongClickListener {
            copyTextToClipboard(this@SettingActivity, DeviceUtils.getUniqueDeviceId())
            true
        }

        switch?.setOnCheckedChangeListener { _, isChecked ->
            if (isChecked) {
                // 开关选中时的逻辑
                ToastUtils.show("功能开发中，敬请期待")
            } else {
                // 开关未选中时的逻辑
                ToastUtils.show("功能开发中，敬请期待")
            }
        }
    }

    private var customPopup: CustomPopup? = null
    private fun showDialog() {
        customPopup = CustomPopup(this, list)
        Log.d("CustomPopup", "-list-list-list--0--size->:" + list.size)
        XPopup.Builder(this).asCustom(customPopup).show()
    }

    private fun copyTextToClipboard(context: Context, text: String?) {
        // 获取系统服务来获取ClipboardManager实例
        val clipboard: ClipboardManager =
            context.getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
        // 创建一个ClipData对象，其中包含要复制的文本
        val clip = ClipData.newPlainText("label", text)
        // 将ClipData对象设置到剪贴板中
        clipboard.setPrimaryClip(clip)
        // 显示一个提示，告知用户文本已被复制
        Toast.makeText(context, "文本已复制到剪贴板", Toast.LENGTH_SHORT).show()
    }

}